package com.hhf.rpc.core.proxy;

import com.hhf.rpc.core.RpcApplication;
import com.hhf.rpc.core.config.RpcConfig;

import java.lang.reflect.Proxy;

/**
 * @Author hhf
 * @Date 2024/10/15
 * @Description
 */
public class ServiceProxyFactory {

    public static <T> T getProxy(Class<T> serviceClass) {

        RpcConfig rpcConfig = RpcApplication.getRpcConfig();

        if (rpcConfig.getMock()){
            return getMockProxy(serviceClass);
        }

        return (T) Proxy.newProxyInstance(
                serviceClass.getClassLoader(),
                new Class[]{serviceClass},
                new ServiceProxy()
        );
    }

    private static <T> T getMockProxy(Class<T> mockServiceClass){
        return (T) Proxy.newProxyInstance(
                mockServiceClass.getClassLoader(),
                new Class[]{mockServiceClass},
                new MockServiceProxy()
        );
    }




}
